<?php

namespace App\Filament\Widgets;

use App\Models\Book;
use App\Models\BorrowRecord;
use Filament\Support\RawJs;
use Filament\Widgets\ChartWidget;

class ViewChart extends ChartWidget
{

    protected static ?int $sort = 1;
    protected static ?string $maxHeight = '300px';
    protected static ?string $heading = '人气图书';
    protected function getOptions(): RawJs|array|null
    {
            return RawJs::make(<<<JS
            {
                scales: {
                    x: {
                        display: false,
                       grid :{
                            display: 'none'
                       },
                       ticks:{
                           display: 'none'
                       }
                    },
                   y: {
                        display:false,
                       grid :{
                            display: 'none'
                       },
                       ticks:{
                           display: 'none'
                       }
                    },
                },
            }
        JS);
    }
    protected function getData(): array
    {
        $borrow = Book::query()->orderBy('views','desc')->limit(5)->get();

        return [
            //
            'labels'=>$borrow->map(fn($item)=> $item->title)->toArray(),
            'datasets'=>[
                [
                    'label'=> 'My First Dataset',
                    'data'=> $borrow->pluck('views')->toArray(),
                    'backgroundColor'=> [
                        'rgb(118, 215, 196)',
                        'rgb(245, 176, 65)',
                        'rgb(255, 87, 51)',
                        'rgb(218, 247, 166)',
                        'rgb(131, 145, 146)'
                    ],
                    'hoverOffset'=> 4
                ]

            ]
        ];
    }

    protected function getType(): string
    {
        return 'pie';
    }
}
